<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="decorator" uri="http://claudiushauptmann.com/jsp-decorator/"%>
<%@ page import="vn.fpt.fsoft.entity.User" %>

<decorator:decorate filename="MasterPage.jsp">
    <decorator:content placeholder="title">Amend Trust District</decorator:content>
    <decorator:content placeholder="head">
<link href="css/bootstrap.min.css"
	rel="stylesheet" type="text/css" />
<link href="css/style-1.css" rel="stylesheet" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript" src="http://getbootstrap.com/dist/js/bootstrap.min.js"></script>
<script type="text/javascript" src="http://bootboxjs.com/bootbox.js"></script>
<script>
	var  regionID;
	function goBack() {
		
		window.location.replace("AmendTrustRegion.jsp?regionID="+<%=request.getParameter("regionID")%>);
	}
	function isValidate() {
		var name = $("#name").val();
		if (name == "") {
			return false;
		} else {
			return true;
		}
	}
	
	function markInActive() {
		var districtID = $("#districtID").val();
        $.ajax({
            url:"AmendTrustDistrict?action=markInActive&districtID=" + districtID,
            type: "GET",
            success:function(data) {
                $("#name").css("border-color", "");
                $("#invalid").html(data);
                $("#invalid").css("color","green");
            },
            error:function(data){
                alert("fail");
            }
        });
	}
	
	function saveDistrict() {
		if (isValidate()) {
			bootbox.confirm("Are you sure you  want to save it?", function(
					result) {
				if (result == true) {
					var districtID =$("#districtID").val();
					var name = $("#name").val();
					var description = $("#description").val();
					$("#invalid").html("");
					$.ajax({
						url : "AmendTrustDistrict?action=amend&districtID="+ districtID
								+ "&name=" + name
								+ "&description=" + description,
						type : "GET",
						success : function(data) {
							$("#name").val("");
							if(data.indexOf("success")>=0)
							{
								$("#name").css("border-color", "");
								$("#invalid").html(data);
								$("#invalid").css("color", "green");
							}else
							{
								$("#name").css("border-color", "red");
								$("#invalid").html(data);
								$("#invalid").css("color", "red");
								$("#name").focus();
							}
						},
						error : function(data) {
							alert("Saving fails!");
							$("#invalid").html(data);
							$("#invalid").css("color", "red");
						}
					});
				} else {

					$("#invalid").html("");
					$("#name").val("");
					$("#description").val("");
					$("#name").focus();
				}
			});

		} else {
			$("#name").css("border-color", "red");
			$("#invalid").html("Trust district name can not null");
		}
	}
	$(document).ready(function() {
		$("#tabs").tabs();
		$(window).load(function() {
			$.ajax({
				url : "AmendTrustDistrict?action=getData&districtID="+<%=request.getParameter("districtID") %>
					+"&regionID="+<%=request.getParameter("regionID")%>,
				type : "GET",
				success : function(data) {
					var obj = JSON.parse(data);
					$("#region").val(obj.regionName);
					$("#name").val(obj.districtName);
					$("#description").val(obj.description);
				},
				error : function(data) {
					alert("fail");
				}
			});
		});


	});
</script>
</decorator:content>
 <decorator:content placeholder="body">
 	<%
		User user = (User) request.getSession().getAttribute("user");
		if(user == null) {
		    response.sendRedirect("login.jsp");
		}
	%>
	<p style="color: #0C3; font-size: 24px; font-weight: bold;">Trust
		District Details</p>
	<div id="tabs" style="width: 795px;">
		<ul>
			<li><a href="#detail1">Detail</a></li>
			<li style="float: right">
			     <input type="button" id="btnInActive" value="In-active "
                    onclick="markInActive();" /> &nbsp;&nbsp;
				<input type="button" value="Save"
					onclick="saveDistrict();" /> &nbsp;&nbsp; 
				<input type="button"
					value="Back" onclick="goBack();" />
			</li>
		</ul>
		<div id="detail1" style="background-color: #EFEFEF;">
			<table style="width: 703px;">
				<tr>
					<td width="206">TrustRegion&nbsp;&nbsp;&nbsp;&nbsp;<label
						style="color: red;">*</label></td>
					<td width="481">
						<input  id="region" type="text" size= 30 name="region" readonly="readonly" />
					</td>
				</tr>
				<tr>
					<td height="64">Trust District Name&nbsp;&nbsp;&nbsp;&nbsp;<label
						style="color: red;">*</label></td>
					<td>
						<input id="name" type="text" size="30"/>
						<input id="districtID" type="hidden" name="districtID" value="<%=request.getParameter("districtID") %>"></input>
					</td>
				</tr>
				<tr>
					<td>Description</td>
					<td><textarea id="description" rows="4" cols="30"></textarea></td>
				</tr>
			</table>
			<br /> <br /> <br />
			<p id="invalid" style="color: #F00; font-size: 14px"></p>
		</div>
	</div>
    </decorator:content>
</decorator:decorate>
